import gradio as gr
from openai import OpenAI
import os

def greet(input):
    api_key = os.getenv("OPENAI_API_KEY")
    base_url = os.getenv("OPENAI_API_BASE")

    print("api_key: %s base_url : %s" % (api_key, base_url))
    timeout = 120

    client = OpenAI(api_key=api_key, base_url=base_url, timeout=timeout)

    messages = [
        {"role": "system", "content": "#角色: 英语翻译，会把所有的语言翻译成英文，并不做任何的解释。"},
        {"role": "user", "content": input}
    ]

    response = client.chat.completions.create(model="gpt-3.5-turbo", messages=messages)
    response_str = response.choices[0].message.content


    return response_str

demo = gr.Interface(
    fn=greet,
    inputs=gr.Textbox(lines=2, placeholder="在这里输入你想要说的话吧！"),
    outputs="text",
)
demo.launch()
